﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimKiemLoaiHang : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        public LOAIHANG loaihangAdd = new LOAIHANG();
        public FormTimKiemMatHang frmTimMatHang;
        public FormThongKeHangTon frmThongKeHangTon;

        public FormTimKiemLoaiHang()
        {
            InitializeComponent();
        }

        public FormTimKiemLoaiHang(FormTimKiemMatHang frm)
        {
            frmTimMatHang = frm;
            InitializeComponent();
            this.btnAdd.Visible = true;
            this.btnSuaLH.Enabled = false;
            this.btnXoaLoaiHang.Enabled = false;                
        }

        public FormTimKiemLoaiHang(FormThongKeHangTon frm)
        {
            frmThongKeHangTon = frm;
            InitializeComponent();
            this.btnAdd.Visible = true;
            this.btnSuaLH.Enabled = false;
            this.btnXoaLoaiHang.Enabled = false;
                 

        }

        private void btnThoatLoaiHang_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTimLoaiHang_Click(object sender, EventArgs e)
        {
            var lh =(from m in db.LOAIHANGs
                         where m.TenLoaiHang.StartsWith(this.txtTimKiemLHang.Text)
                         select m).ToList();
            this.lOAIHANGBindingSource.DataSource=lh;
            //this.lOAIHANGBindingSource.ResetBindings(true); ?????
            this.dataGridViewTkiemLH.DataSource = lOAIHANGBindingSource;
        }
      
        private void FormTimKiemLoaiHang_Load(object sender, EventArgs e)
        {
            this.dataGridViewTkiemLH.AutoGenerateColumns = false;
            var loaihang = (from lh in db.LOAIHANGs
                            orderby lh.MaLoaiHang ascending
                            select lh
                                ).ToList();
            this.lOAIHANGBindingSource.DataSource = loaihang;
            this.dataGridViewTkiemLH.DataSource = this.lOAIHANGBindingSource;
        }

        public LOAIHANG LoaiHangSua = new LOAIHANG();
        private void btnSuaLH_Click(object sender, EventArgs e)
        {
            var loaihang = this.lOAIHANGBindingSource.Current as LOAIHANG;
            if (loaihang != null)
            {
                LoaiHangSua = loaihang;
                var kq = new FormChiTietLoaiHang(LoaiHangSua, this).ShowDialog();
                if (kq == DialogResult.OK)
                {
                    loaihang.TenLoaiHang = LoaiHangSua.TenLoaiHang;
                    this.lOAIHANGBindingSource.ResetCurrentItem();
                    MessageBox.Show("Đã cập nhật dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        private void btnXoaLoaiHang_Click(object sender, EventArgs e)
        {
            var loaihang = this.lOAIHANGBindingSource.Current as LOAIHANG;
            if (loaihang != null)
            {
                var hang = (from m in db.MATHANGs
                            where m.MaLoaiHang == loaihang.MaLoaiHang
                            select m).ToList();
                if (hang.Count > 0)
                {
                    MessageBox.Show("Loại hàng này có chứa các mặt hàng, vui lòng xóa hàng trước khi xóa loại hàng này");
                    return;
                }
                else
                {
                    try
                    {
                        var tl = MessageBox.Show("Bạn có chắc chắn xóa", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (tl == DialogResult.Yes)
                        {
                            db.LOAIHANGs.DeleteOnSubmit(loaihang);
                            db.SubmitChanges();
                            this.lOAIHANGBindingSource.RemoveCurrent();
                            MessageBox.Show("Xóa thành công!");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Đã có lỗi xảy ra khi xóa chi tiết lỗi : " + ex.Message);
                    }
                }
            }
            else
            {
                MessageBox.Show("Chọn loại hàng cần xóa");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var loaihang = this.lOAIHANGBindingSource.Current as LOAIHANG;
            if (loaihang != null)
            {
                // this.frmLapChungTu.txtMaKH.Text = khachhang.MaKhachHang;
                if (frmTimMatHang != null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.frmTimMatHang.comboBoxLH.Text = loaihang.MaLoaiHang;
                    this.Close();
                }

                if (frmThongKeHangTon != null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.frmThongKeHangTon.comboBoxloaihang.Text = loaihang.MaLoaiHang;
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn nhà cung cấp");
            }
        }
    }
}
